/// @Classname : HcCellGroup
/// @Author :
/// @Time : 2023-09-19 11:07
/// @Description : //todo

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_cell_group_theme.dart';
import 'package:hc_ui/widget/grid/hc_grid.dart';

import '../divider/hc_divider.dart';

enum HcCellType { list, grid }

typedef HcCellGroupTap = Function(String type, int index, String? title);

class HcCellGroup extends StatelessWidget {
  //需要展示的类型
  final HcCellType cellType;

  //展示的列数
  final int rowCounts;

  //组件列表
  final List<Widget> children;

  //点击的回调
  final HcCellGroupTap? onTap;

  //是否展示箭头
  final bool showArrow;

//  自定义分割线
  final Widget? dividerWidget;

  //内部的标题
  final Widget? insideTitle;

  //分割线样式
  final HcDividerStyle dividerStyle;

  final HcCellGroupTheme? themeData;

  const HcCellGroup({
    super.key,
    this.cellType = HcCellType.list,
    this.rowCounts = 1,
    required this.children,
    this.onTap,
    this.showArrow = true,
    this.dividerWidget,
    this.insideTitle,
    this.dividerStyle = HcDividerStyle.solid,
    this.themeData,
  });

  static HcCellGroupScope? of(BuildContext context) {
    Widget? widget = context
        .getElementForInheritedWidgetOfExactType<HcCellGroupScope>()
        ?.widget;
    return widget != null ? widget as HcCellGroupScope : null;
  }

  @override
  Widget build(BuildContext context) {
    HcCellGroupTheme theme = themeData ?? HcCellGroupTheme();
    theme = HcCustomerConfig().allTheme.cellGroupTheme.merge(theme);
    return Container(
      margin: theme.margin,
      child: Ink(
        padding: theme.padding,
        decoration: BoxDecoration(
            border: theme.border,
            borderRadius: theme.borderRadius,
            color: theme.backgroundColor),
        child: Column(
          children: [
            if (insideTitle != null)
              GestureDetector(
                onTap:
                    onTap != null ? () => onTap!('insideTitle', 0, null) : null,
                child: insideTitle!,
              ),
            HcGrid(
                dividerThickness: theme.dividerWidth,
                dividerColor: theme.dividerColor,
                dividerStyle: dividerStyle,
                leftIndent: theme.leftIndent,
                rightIndent: theme.rightIndent,
                itemBuilder: (context, index) {
                  return HcCellGroupScope(
                      type: cellType,
                      radius: theme.borderRadius,
                      rowCounts: cellType == HcCellType.list ? 1 : rowCounts,
                      showArrow: showArrow,
                      callback: onTap,
                      showInsideTitle: insideTitle != null,
                      child: children[index],
                      children: children);
                },
                crossAxisCount: cellType == HcCellType.list ? 1 : rowCounts,
                itemCount: children.length),
          ],
        ),
      ),
    );
  }
}

//向子组件传递内容
class HcCellGroupScope extends InheritedWidget {
  //组件的全部列表
  final List<Widget> children;

  //圆角矩形
  final BorderRadius radius;

  //是否展示箭头
  final bool showArrow;

  //点击按钮的回调
  final HcCellGroupTap? callback;

  //当前单元格展示类型
  final HcCellType type;

  //每行展示的个数
  final int rowCounts;

  //是否展示内部标题
  final bool showInsideTitle;

  const HcCellGroupScope(
      {super.key,
      required this.rowCounts,
      required this.children,
      required this.radius,
      required this.showArrow,
      this.callback,
      required this.showInsideTitle,
      required this.type,
      required super.child});

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    return oldWidget.child != child;
  }
}
